package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseSaleAttr;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @author: JC
 * @date: 2021/10/23 19:32
 */

@Api(description = "Spu控制器")
@RestController
@RequestMapping("admin/product")
public class SpuManageController {
    // 引入服务层
    @Autowired
    private ManageService manageService;

    // 销售属性http://api.gmall.com/admin/product/baseSaleAttrList
    @GetMapping("baseSaleAttrList")
    public Result baseSaleAttrList(){
        // 查询所有的销售属性集合
        List<BaseSaleAttr> baseSaleAttrList = manageService.getBaseSaleAttrList();

        return Result.ok(baseSaleAttrList);
    }

    /**
     * http://api.gmall.com/admin/product/{page}/{limit}?category3Id=61
     */
    @GetMapping("/{page}/{limit}")
    public Result getSpuInfoList(@PathVariable("page")Long page,
                                 @PathVariable("limit")Long limit,
                                 String category3Id){
        // 分页查询
        Page<SpuInfo> objectPage = new Page(page, limit);

        IPage iPage= manageService.getSpuInfoList(objectPage,category3Id);

        return Result.ok(iPage);
    }

    /**
     * 保存spu
     * @param spuInfo
     * @return
     */
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        // 调用服务层的保存方法
        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }




    @GetMapping("/spuImageList/{spuId}")
    public Result getSpuImageListById(@PathVariable("spuId") Long spuId){

       List<SpuImage> spuImageList =  manageService.getSpuImageListById(spuId);

       return  Result.ok(spuImageList);
    }

    @GetMapping("spuSaleAttrList/{spuId}")
    public Result spuSaleAttrListById(@PathVariable("spuId") Long spuId){

       List<SpuSaleAttr> spuSaleAttrList =  manageService.spuSaleAttrListById(spuId);

       return  Result.ok(spuSaleAttrList);
    }


}
